package models

import (
	"context"
	. "gin-chat/entity"
	"time"
)

type _CGroupMgr struct {
	*_BaseMgr
}

// CGroupMgr open func
func CGroupMgr() *_CGroupMgr {
	ctx, cancel := context.WithCancel(context.Background())
	return &_CGroupMgr{_BaseMgr: &_BaseMgr{DB: db.Table("c_group"), isRelated: globalIsRelated, ctx: ctx, cancel: cancel, timeout: -1}}
}

// GetTableName get sql table name.获取数据库名字
func (obj *_CGroupMgr) GetTableName() string {
	return "c_group"
}

// Get 获取
func (obj *_CGroupMgr) Get() (result CGroup, err error) {
	err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Find(&result).Error

	return
}

// Gets 获取批量结果
func (obj *_CGroupMgr) Gets() (results []*CGroup, err error) {
	err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Find(&results).Error

	return
}

//////////////////////////option case ////////////////////////////////////////////

// WithID id获取
func (obj *_CGroupMgr) WithID(id int) Option {
	return optionFunc(func(o *options) { o.query["id"] = id })
}

// WithName name获取
func (obj *_CGroupMgr) WithName(name string) Option {
	return optionFunc(func(o *options) { o.query["name"] = name })
}

// WithGroupID group_id获取 群id
func (obj *_CGroupMgr) WithGroupID(groupID int64) Option {
	return optionFunc(func(o *options) { o.query["group_id"] = groupID })
}

// WithAvatarURL avatar_url获取
func (obj *_CGroupMgr) WithAvatarURL(avatarURL string) Option {
	return optionFunc(func(o *options) { o.query["avatar_url"] = avatarURL })
}

// WithCreateTime create_time获取 创建时间
func (obj *_CGroupMgr) WithCreateTime(createTime time.Time) Option {
	return optionFunc(func(o *options) { o.query["create_time"] = createTime })
}

// WithUpdateTime update_time获取 修改时间
func (obj *_CGroupMgr) WithUpdateTime(updateTime time.Time) Option {
	return optionFunc(func(o *options) { o.query["update_time"] = updateTime })
}

// WithCreateBy create_by获取
func (obj *_CGroupMgr) WithCreateBy(createBy string) Option {
	return optionFunc(func(o *options) { o.query["create_by"] = createBy })
}

// WithCreateByID create_by_id获取 创建人id
func (obj *_CGroupMgr) WithCreateByID(createByID int) Option {
	return optionFunc(func(o *options) { o.query["create_by_id"] = createByID })
}

// WithAdministrator administrator获取 群主id
func (obj *_CGroupMgr) WithAdministrator(administrator int) Option {
	return optionFunc(func(o *options) { o.query["administrator"] = administrator })
}

// GetByOption 功能选项模式获取
func (obj *_CGroupMgr) GetByOption(opts ...Option) (result CGroup, err error) {
	options := options{
		query: make(map[string]interface{}, len(opts)),
	}
	for _, o := range opts {
		o.apply(&options)
	}

	err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Where(options.query).Find(&result).Error

	return
}

// GetByOptions 批量功能选项模式获取
func (obj *_CGroupMgr) GetByOptions(opts ...Option) (results []*CGroup, err error) {
	options := options{
		query: make(map[string]interface{}, len(opts)),
	}
	for _, o := range opts {
		o.apply(&options)
	}

	err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Where(options.query).Find(&results).Error

	return
}

//////////////////////////enume case ////////////////////////////////////////////

// GetFromID 通过id获取内容
func (obj *_CGroupMgr) GetFromID(id int) (result CGroup, err error) {
	err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Where("`id` = ?", id).Find(&result).Error

	return
}

// GetBatchFromID 批量查找
func (obj *_CGroupMgr) GetBatchFromID(ids []int) (results []*CGroup, err error) {
	err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Where("`id` IN (?)", ids).Find(&results).Error

	return
}

// GetFromName 通过name获取内容
func (obj *_CGroupMgr) GetFromName(name string) (results []*CGroup, err error) {
	err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Where("`name` = ?", name).Find(&results).Error

	return
}

// GetBatchFromName 批量查找
func (obj *_CGroupMgr) GetBatchFromName(names []string) (results []*CGroup, err error) {
	err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Where("`name` IN (?)", names).Find(&results).Error

	return
}

// GetFromGroupID 通过group_id获取内容 群id
func (obj *_CGroupMgr) GetFromGroupID(groupID int64) (results []*CGroup, err error) {
	err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Where("`group_id` = ?", groupID).Find(&results).Error

	return
}

// GetBatchFromGroupID 批量查找 群id
func (obj *_CGroupMgr) GetBatchFromGroupID(groupIDs []int64) (results []*CGroup, err error) {
	err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Where("`group_id` IN (?)", groupIDs).Find(&results).Error

	return
}

// GetFromAvatarURL 通过avatar_url获取内容
func (obj *_CGroupMgr) GetFromAvatarURL(avatarURL string) (results []*CGroup, err error) {
	err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Where("`avatar_url` = ?", avatarURL).Find(&results).Error

	return
}

// GetBatchFromAvatarURL 批量查找
func (obj *_CGroupMgr) GetBatchFromAvatarURL(avatarURLs []string) (results []*CGroup, err error) {
	err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Where("`avatar_url` IN (?)", avatarURLs).Find(&results).Error

	return
}

// GetFromCreateTime 通过create_time获取内容 创建时间
func (obj *_CGroupMgr) GetFromCreateTime(createTime time.Time) (results []*CGroup, err error) {
	err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Where("`create_time` = ?", createTime).Find(&results).Error

	return
}

// GetBatchFromCreateTime 批量查找 创建时间
func (obj *_CGroupMgr) GetBatchFromCreateTime(createTimes []time.Time) (results []*CGroup, err error) {
	err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Where("`create_time` IN (?)", createTimes).Find(&results).Error

	return
}

// GetFromUpdateTime 通过update_time获取内容 修改时间
func (obj *_CGroupMgr) GetFromUpdateTime(updateTime time.Time) (results []*CGroup, err error) {
	err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Where("`update_time` = ?", updateTime).Find(&results).Error

	return
}

// GetBatchFromUpdateTime 批量查找 修改时间
func (obj *_CGroupMgr) GetBatchFromUpdateTime(updateTimes []time.Time) (results []*CGroup, err error) {
	err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Where("`update_time` IN (?)", updateTimes).Find(&results).Error

	return
}

// GetFromCreateBy 通过create_by获取内容
func (obj *_CGroupMgr) GetFromCreateBy(createBy string) (results []*CGroup, err error) {
	err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Where("`create_by` = ?", createBy).Find(&results).Error

	return
}

// GetBatchFromCreateBy 批量查找
func (obj *_CGroupMgr) GetBatchFromCreateBy(createBys []string) (results []*CGroup, err error) {
	err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Where("`create_by` IN (?)", createBys).Find(&results).Error

	return
}

// GetFromCreateByID 通过create_by_id获取内容 创建人id
func (obj *_CGroupMgr) GetFromCreateByID(createByID int) (results []*CGroup, err error) {
	err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Where("`create_by_id` = ?", createByID).Find(&results).Error

	return
}

// GetBatchFromCreateByID 批量查找 创建人id
func (obj *_CGroupMgr) GetBatchFromCreateByID(createByIDs []int) (results []*CGroup, err error) {
	err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Where("`create_by_id` IN (?)", createByIDs).Find(&results).Error

	return
}

// GetFromAdministrator 通过administrator获取内容 群主id
func (obj *_CGroupMgr) GetFromAdministrator(administrator int) (results []*CGroup, err error) {
	err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Where("`administrator` = ?", administrator).Find(&results).Error

	return
}

// GetBatchFromAdministrator 批量查找 群主id
func (obj *_CGroupMgr) GetBatchFromAdministrator(administrators []int) (results []*CGroup, err error) {
	err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Where("`administrator` IN (?)", administrators).Find(&results).Error

	return
}

//////////////////////////primary index case ////////////////////////////////////////////

// FetchByPrimaryKey primay or index 获取唯一内容
func (obj *_CGroupMgr) FetchByPrimaryKey(id int) (result CGroup, err error) {
	err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Where("`id` = ?", id).Find(&result).Error

	return
}
